package com.gisuni.sddc.platformManager.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.GET;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import com.gisuni.sddc.platformManager.model.ModuleRLayer;
import com.gisuni.sddc.platformManager.model.Response;
import com.gisuni.sddc.platformManager.pageModel.Tree;
import com.gisuni.sddc.platformManager.service.ServerResourceService;

@IocBean(singleton=false)
@At("/client/backgroundManagement/ServerResourceController")
public class ServerResourceController {
//	private Log log = Logs.getLog(UserController.class);
	private  Log log = Logs.getLog(ServerResourceController.class);

	@Inject  
	private ServerResourceService  serverResourceService;
	
	@At("/tree")
	@GET
	@POST
	@Ok("json")
	public Object allTreeNode() {
		List<Tree> serverList=new ArrayList<Tree>();
		try{
			serverList= serverResourceService.allTreeNode();
		}catch(Exception e){
			log.error(e.getMessage());
		}
		return serverList;
	}
	
	@At("/modelTree")
	@GET
	@POST
	@Ok("json")
	public Object allModelTreeNode() {
		List<Tree> moduleList=new ArrayList<Tree>();
		try{
			moduleList=serverResourceService.allModelTreeNode();
		}catch(Exception e){
			log.error(e.toString());
		}
		return moduleList;
	}
	
	@At("/moduleAndParameters/")
	@GET
	@POST
	@Ok("json")
	public Object moduleAndParametersField(@Param(value = "id") String Id,@Param(value = "pid") String Pid){
		Response response=new Response();
		response.setSuccess(false);
		List<ModuleRLayer> moAndPa=new ArrayList<ModuleRLayer>();
		try{
			moAndPa=serverResourceService.getConfFieldsByid(Id,Pid);
			response.setResult(moAndPa);
			response.setMsg("获取模块参数成功！");
			response.setSuccess(true);
		}catch(Exception e){
			response.setMsg("获取模块参数失败！");
			log.error(e.getMessage());
		}
		return response;
	}
	
	@At("/getLayerInfoByIds/")
    @GET
    @POST
    @Ok("json")
    public Object getLayerInfoByIds(@Param(value = "pid") String serverId,@Param(value = "id") String layerId){
		Response response=new Response();
		response.setSuccess(false);
		Object object=new Object();
		try{
			object=serverResourceService.getLayerInfosByIds(serverId,layerId);
			response.setResult(object);
			response.setMsg("获取图层信息成功");
			response.setSuccess(true);
		}catch(Exception e){
			response.setMsg("获取图层信息失败");
			log.error(e.getMessage());
		}
    	return object;
    }
	@At("/addActionServerConfig/")
	@GET
	@POST
	@Ok("json")
	public Object addActionServerConfig(@Param(value="moduleId")String moduleId,@Param(value="layerId")String layerId,
			@Param(value="fieldsExpress") String fieldsExpress,@Param(value="layerParam") String layerParam){
		Response response=new Response();
		response.setSuccess(false);
		try{
			boolean msg= serverResourceService.insertOrUpdateActionServerConfig(moduleId,layerId,fieldsExpress,layerParam);
			if(msg==true){
				response.setSuccess(true);
				response.setMsg("保存配置成功");
			}else{
				response.setSuccess(true);
				response.setMsg("保存配置失败");
			}
		  }catch(Exception e){
			  response.setMsg("保存配置失败");
			  log.error(e.getMessage());
		  }
		return response;
		}
	@At("/getModuleServerConfigByModuleIdAndLayerParam/")
	@GET
	@POST
	@Ok("json")
	public Object getModuleServerConfigByModuleIdAndLayerParam(@Param(value="ModuleId") String moduleId,@Param(value="layerParam") String layerParam){
		Response response=new Response();
		response.setSuccess(false);
		try{
			ModuleRLayer moduleRLayer =serverResourceService.getModuleServerConfigByMAL(moduleId,layerParam);
			if(moduleRLayer==null){
				response.setMsg("该模块尚未配置，请配置！");
			}else{
				response.setResult(moduleRLayer);
				response.setSuccess(true);
				response.setMsg("读取配置成功！");
			}
		 }catch(Exception e){
			 response.setMsg("读取配置失败！");
			 log.error(e.getMessage());
		 }
		return response;
	 }
	


}
  
